package com.pansoft.kline.util;

import com.efounder.utils.CommonDecimalFormat;
import com.efounder.utils.ResStringUtil;
import com.pansoft.openplanet.R;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.util.Locale;

/**
 * 币看的数据处理工具类
 * 从jar包拷贝出来的
 *
 * @autor yqs
 * @date 2018/11/22 9:26
 **/
public class BtcKanNumberFormatUtil {


    private static CommonDecimalFormat cnFormat = new CommonDecimalFormat("0.0000000000000000", DecimalFormatSymbols.getInstance());
    private static CommonDecimalFormat usFormat;
    private static CommonDecimalFormat c;

    private static CommonDecimalFormat percentFormat = new CommonDecimalFormat("0.00%");

    static {
        cnFormat.setGroupingSize(0);

        //https://blog.csdn.net/well386/article/details/53945796/
//       RoundingMode.FLOOR  向负无限大方向舍入的舍入模式。如果结果为正，则舍入行为类似于 RoundingMode.DOWN；如果结果为负，则舍入行为类似于RoundingMode.UP。注意，此舍入模式始终不会增加计算值。
//示例：
//
//输入数字	使用 FLOOR 舍入模式
//将输入数字舍入为一位数
//5.5	5
//2.5	2
//1.6	1
//1.1	1
//1.0	1
//-1.0	-1
//-1.1	-2
//-1.6	-2
//-2.5	-3
//-5.5	-6
        cnFormat.setRoundingMode(RoundingMode.FLOOR);
        usFormat = new CommonDecimalFormat("0.0000000000000000", DecimalFormatSymbols.getInstance(Locale.US));
        usFormat.setGroupingSize(0);
        usFormat.setRoundingMode(RoundingMode.FLOOR);
        c = new CommonDecimalFormat("0.0", DecimalFormatSymbols.getInstance());
    }

    public BtcKanNumberFormatUtil() {
    }

    public static String assignNumberFormat(double number) {
        return (new DecimalFormat("0.########")).format(number);
    }

    public static String assignNumberFormat(double number, int length) {
        return assignNumberFormat(number, length, cnFormat);
    }

    public static String assignNumberFormat(double number, int length, int minimumCount) {

        return assignNumberFormat(number, length, minimumCount, cnFormat);
    }


    public static void main(String[] args) {
        //  String s = assignNumberFormat(3289700.122, 7, 7);
        // assignNumberFormat(8.1, 7, 7);

//      assignNumberFormat(100.444, 7, 7);
//        System.out.println(assignNumberFormat(31863.26, 7, 7));
//        System.out.println(assignNumberFormat(31863.26, 6, 6));


        System.out.println(assignNumberFormat(7.971612345, 7, 5));
        System.out.println(assignNumberFormat(7.97, 7, 5));
        System.out.println(assignNumberFormat(79.971612345, 7, 5));
        System.out.println(assignNumberFormat(79.97, 7, 5));

        System.out.println(assignNumberFormat(31863.2612345, 7, 5));
        System.out.println(assignNumberFormat(31863.2, 7, 5));

    }


    /**
     * 获取百分比
     *
     * @param number
     * @return
     */
    public static String getPercent(double number) {
        String result = percentFormat.format(number);
        return result;
    }

    /**
     * 将数字转化为指定的长度
     *
     * @param number       数字
     * @param length       数字长度
     * @param minimumCount 小数最小位数
     * @param numberFormat
     * @return
     */
    public static String assignNumberFormat(double number, int length, int minimumCount, NumberFormat numberFormat) {
        //设置数值的小数部分允许的最小位数
        numberFormat.setMaximumFractionDigits(minimumCount);
        // 设置数值的小数部分允许的最小位数
        numberFormat.setMinimumFractionDigits(minimumCount);
        String formatString = numberFormat.format(number);
        if (length + 1 < formatString.length()) {
            formatString = formatString.substring(0, length + 1);
        }

        char var6 = formatString.charAt(formatString.length() - 1);
        String resultString = formatString;
        if (formatString.length() > 0) {
            if (var6 != '.') {
                resultString = formatString;
                if (var6 != ',') {
                    return resultString;
                }
            }
            resultString = formatString.substring(0, formatString.length() - 1);
        }

        return resultString;
    }

    public static String assignNumberFormat(double number, int maxCount, NumberFormat numberFormat) {
        numberFormat.setMaximumFractionDigits(maxCount);
        numberFormat.setMinimumFractionDigits(0);
        numberFormat.setRoundingMode(RoundingMode.CEILING);
        return numberFormat.format(number);
    }

    /**
     * 保留两位小数
     *
     * @param var0
     * @return
     */
    public static String keep2Decimal(float var0) {
        return (new DecimalFormat("0.00")).format((double) var0);
    }


    public static String b(double var0) {
        return (new DecimalFormat("0.####")).format(var0);
    }

    public static String b(double var0, int var2) {
        return String.format("%.2f", 100.0D * var0) + "%";
    }

    public static String b(double var0, int var2, int var3) {
        String var4;
        if (var0 == -1.0D) {
            var4 = "--";
        } else {
            var4 = assignNumberFormat(var0, var2, var3, cnFormat);
        }

        return var4;
    }

    /**
     * 添加数字符号
     *
     * @param var0
     * @return
     */
    public static String addNumberSign(String var0) {
        var0 = var0.trim();
        if (!var0.startsWith("-")) {
            var0 = "+" + var0;
        }

        return var0;
    }

    public static String c(double var0) {
        return (new DecimalFormat("0.##")).format(var0);
    }

    public static String c(double var0, int var2, int var3) {
        return assignNumberFormat(var0, var2, var3, usFormat);
    }


    public static String d(double var0, int var2, int var3) {
        String var4;
        if (var0 < 1.0E-5D) {
            var4 = assignNumberFormat(1000.0D * var0, var2, var3) + 'm';
        } else {
            var4 = assignNumberFormat(var0, var2, var3);
        }

        return var4;
    }


    public static String e(double var0) {
        cnFormat.setMaximumFractionDigits(1);
        cnFormat.setMinimumFractionDigits(2);
        cnFormat.setRoundingMode(RoundingMode.HALF_UP);
        String var2;
        // if (ar.assignNumberFormat(e.assignNumberFormat().E()).equals("zh")) {
        if (Math.abs(var0) > 1.0E8D) {
            var2 = cnFormat.format(var0 / 1.0E8D) + " "+ResStringUtil.getString(R.string.common_text_billion);
            return var2;
        }

        if (Math.abs(var0) > 10000.0D) {
            var2 = cnFormat.format(var0 / 10000.0D) + " "+ResStringUtil.getString(R.string.common_text_ten_thousand);
            return var2;
        }
//        } else {
//            if (Math.abs(var0) > 1.0E9D) {
//                var2 = cnFormat.format(var0 / 1.0E9D) + "B";
//                return var2;
//            }
//
//            if (Math.abs(var0) > 1000000.0D) {
//                var2 = cnFormat.format(var0 / 1000000.0D) + "M";
//                return var2;
//            }
//
//            if (Math.abs(var0) > 1000.0D) {
//                var2 = cnFormat.format(var0 / 1000.0D) + "K";
//                return var2;
//            }
//        }

        var2 = cnFormat.format(var0);
        return var2;
    }


    public static String f(double var0) {
        return assignNumberFormat(var0, 7, 5);
    }


    public static String g(double var0) {
        String var2;
        if (var0 == -1.0D) {
            var2 = "--";
        } else {
            var2 = assignNumberFormat(var0, 7, 5);
        }

        return var2;
    }


    public static String h(double var0) {
        return (new DecimalFormat("#,###")).format(var0);
    }


    public static String i(double var0) {
        return (new DecimalFormat("#,##0.00")).format(var0);
    }


    public static String j(double var0) {
        return (new DecimalFormat("0.########")).format(var0);
    }

    public static String k(double var0) {
        return assignNumberFormat(var0, 7, 2);
    }


    public static String m(double var0) {
        return assignNumberFormat(var0, 6, 3);
    }

    public static String n(double var0) {
        return assignNumberFormat(100.0D * var0, 2) + "%";
    }

    public static String o(double var0) {
        String var2;
        if (var0 > 0.0D) {
            var2 = "+";
        } else {
            var2 = "";
        }

        return var2 + assignNumberFormat(100.0D * var0, 8) + "%";
    }

    public static String p(double var0) {
        String var2;
        if (var0 > 0.0D) {
            var2 = "+";
        } else {
            var2 = "";
        }

        return var2 + k(var0);
    }

    public static String q(double var0) {
        return assignNumberFormat(100.0D * var0, 8);
    }

    public static String r(double var0) {
        return c.format(var0);
    }


    //这个方法用来处理平台币item 右侧下方数据
    public static String formatNumberForSite(String number) {
        //经过比对，币看最多保留十位小数，并且四舍五入
        if (!number.contains(".")) {
            return number;
        }
        //判断小数有几位
        int length = number.substring(number.lastIndexOf(".") + 1, number.length()).length();
        if (length > 10) {
            //只截取10位小数
            number = number.substring(0, number.lastIndexOf(".") + 1 + 10);
        }
        //再次获取小数有几位
        length = number.substring(number.lastIndexOf(".") + 1, number.length()).length();

        //判断 number>100000 保留一位 >10000 保留两位小数，>1000保留三位  > 100 保留四位
        double value = Double.valueOf(number);

        if (value >= 1E5D && length > 1) {
            return assignNumberFormat(value, 12, 1);
        }
        if (value >= 1E4D && value < 1E5D && length > 2) {
            return assignNumberFormat(value, 12, 2);
        }
        if (value >= 1000 && value < 1E4D && length > 3) {
            return assignNumberFormat(value, 12, 3);
        }

        if (value >= 100 && value < 1000 && length > 4) {
            return assignNumberFormat(value, 12, 4);
        }

        String result = new BigDecimal(value + "").toPlainString();
        System.out.println(result);
        return result;
    }
}
